<?php

// 查询代付余额控制器

namespace app\api\controller;

use app\api\controller\Base;
use app\api\model\PaymentPaidCompany;
use app\api\model\PaymentOrderPaid;
use app\api\model\PaymentOrderAlipayPaid;
use app\api\model\PaymentRedis;
use think\facade\Cache;

class QueryPaidCash extends Base
{

    /**
     * 代付余额
     */
    public function query()
    {
        try {
            // 只允许命令行访问
            if (PHP_SAPI != 'cli') {
                return ['code' => 999, 'msg' => '404'];
            }

            $redis = Cache::store('redis');

            $map = [];
            $map[] = ['auto_query_cash', '=', 1];
            // $map[] = ['paid_class', '=', 1];

            $all_child_company = PaymentPaidCompany::where($map)
                ->order(['group' => 'asc', 'sort' => 'asc', 'title' => 'asc', 'name' => 'asc'])
                ->column('*', 'name');

            foreach ($all_child_company as $company_k => $company_v) {

                $company_parent_name = strstr($company_k, 'Paid', true);

                $companys = $company_parent_name . 'PaidQueryCash';

                // 实例化第三方订单模型
                if ($company_v['paid_class'] == 0) {
                    $other_order_obj = new PaymentOrderPaid();
                } else {
                    $other_order_obj = new PaymentOrderAlipayPaid();
                }

                // 发送给第三方支付公司订单号    
                $resarray = $other_order_obj->$companys($company_v);

                if ($resarray['code'] == 200) {

                    $now_day = date('Y-m-d');
                    $temp = [];
                    $temp[] = ['child_company', '=', $company_k];
                    $temp[] = ['status', '=', 2];
                    $temp[] = ['create_time', 'between time', ["$now_day 00:00:00", "$now_day 23:59:59"]];
                    if ($company_v['paid_class'] == 0) {
                        $now_order_cash_ok = PaymentOrderPaid::where($temp)->sum('cash');
                    } else {
                        $now_order_cash_ok = PaymentOrderAlipayPaid::where($temp)->sum('cash');
                    }
                    $now_order_cash_ok /= 100;

                    $cash = $resarray['cash'] + $now_order_cash_ok - $resarray['cash'] * $company_v['paid_rate'];
                    $cash = intval($cash);

                    PaymentPaidCompany::where(['name' => $company_k])
                        ->update(['day_paid_cash_sum' => $cash, 'update_time' => time()]);

                    // 更新redis
                    $redis_find = PaymentRedis::where(['name' => $company_k])->find();
                    if ($redis_find) {

                        if (!is_null(json_decode($redis_find['value']))) {
                            $redis_temp_v = json_decode($redis_find['value'], true);
                            $redis_temp_v['day_paid_cash_sum'] = $cash;

                            PaymentRedis::where(['name' => $company_k])
                                ->update(['value' => json_encode($redis_temp_v), 'status' => 1, 'update_time' => time()]);
                        }

                        $redis_config = PaymentRedis::where(['name' => $company_k])
                            ->order(['name' => 'asc'])
                            ->column('*', 'name');
                        foreach ($redis_config as $k => &$v) {
                            //json转换成数组
                            if (!is_null(json_decode($v['value']))) {
                                $v['value'] = json_decode($v['value'], true);
                            }
                            $redis->set($k, $v);
                        }
                    }
                } else {
                    // 查询余额错误
                }
                sleep(10);
            }
            //  
        } catch (\Exception $e) {
            payment_error_log(780000, $e->getMessage());
            return ['code' => 999, 'msg' => '404'];
        }
    }
}
